package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Recharge;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.UserAccountLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.RechargeService;
import com.tbit.uqbike.service.business.UserAccountLogService;
import com.tbit.uqbike.service.business.UserService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 账号费用变化记录
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/userAccountLog", produces = BaseController.MEDIA_JSON_UTF8)
public class UserAccountLogController extends BaseController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private UserAccountLogService userAccountLogService;
    @Autowired
    private UserService userService;
    @Autowired
    private RechargeService rechargeService;

    /* 分页查询 */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(@RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                @NotNull(message = "ver.phone.notNull") String phone,
                                Integer userId, Integer type,
                                HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        Integer accountId;
        if (loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            accountId = null;
        } else if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            accountId = accountService.getBrandId(loginInfo.id);
        }

        List<Integer> userIds = new ArrayList<>();
        List<User> users = userService.getByAccountIdAndPhone(accountId, phone);
        if (users == null || users.size() < 1) {
            throw new BaseException(SystemCodeEnum.USER_NOT_EXIT);
        } else {
            for (User user : users) {
                userIds.add(user.getUserId());
            }
        }
        /*获取总数*/
        Integer total = userAccountLogService.queryCount(accountId, userIds, type);
        /*当前返回行数*/
        List<UserAccountLog> userAccountLogs = userAccountLogService.queryPage(accountId, userIds, type, rowCount, pageNO);

        return JsonResult.ok(userAccountLogs, total);
    }

    /**
     * @param userId
     * @param time
     * @return
     */
    @RequestMapping("/getOrderNO")
    public JsonResult getOrderNO(Integer userId, String time) {
        Assert.notNull(userId, "ver.userId.notNull");
        Assert.notNull(time, "ver.startTime.notNull");
        List<Recharge> list = rechargeService.getByTime(userId, time);
        return JsonResult.ok(list);
    }

}